package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.dto.Query;
import com.itheima.domain.entity.Info;
import com.itheima.domain.vo.InfoVO;
import com.itheima.mapper.InfoMapper;
import com.itheima.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;



@Service
public class InfoServiceImpl extends ServiceImpl<InfoMapper, Info> implements InfoService{
    @Autowired
    private InfoMapper infoMapper;
    @Override
    public List<InfoVO> query(Query query) {
        IPage<Info> page=new Page<>(query.getPageNum(),query.getPageSize());
        QueryWrapper<Info> wrapper = new QueryWrapper<>();
        if (query.getCategory() != null) {
          wrapper.eq("category", query.getCategory());
        }
        if (query.getCity() != null) {
           wrapper.eq("city", query.getCity());
        }
        if (query.getEducation() != null) {
           wrapper.eq("education", query.getEducation());
        }
        if (query.getExperience() != null) {
           wrapper.eq("experience", query.getExperience());
        }
        page = infoMapper.selectPage(page, wrapper);
        List<Info> records = page.getRecords();
        return getInfos(records);
    }
    static List<InfoVO> getInfos(List<Info> records) {
        List<InfoVO> infoVOS = new ArrayList<>();
        for (Info info : records) {
            String requirement= info.getRequirement();
            String[] requirementList = requirement.split("\\|");
            List<String> list = new ArrayList<>(Arrays.asList(requirementList));
            list.remove("<null>");
            InfoVO build = new InfoVO().builder()
                    .jobName(info.getJobName())
                    .salary(info.getSalary())
                    .address(info.getAddress())
                    .experience(info.getExperience())
                    .education(info.getEducation())
                    .requirement(list)
                    .company(info.getCompany())
                    .build();
            infoVOS.add(build);
        }
        return infoVOS;
    }
/*    private static List<InfoVO> getResult(Query query) throws SQLException {
        HiveUtil hiveUtil = new HiveUtil();
        StringBuffer sql = new StringBuffer("select * from ods_zhaoping_info where id>=0");
        if (query.getCategory() != null) {
            sql.append(" and category='").append(query.getCategory()).append("'");
        }
        if (query.getCity() != null) {
            sql.append(" and city='").append(query.getCity()).append("'");
        }
        if (query.getEducation() != null) {
            sql.append(" and education='").append(query.getEducation()).append("'");
        }
        if (query.getExperience() != null) {
            sql.append(" and experience='").append(query.getExperience()).append("'");
        }
        sql.append(" limit ").append(query.getPageSize());
        sql.append(" offset ").append((query.getPageNum()-1)*query.getPageSize());
        String str= sql.toString();
        List<InfoVO> infoVOS = new ArrayList<>();
        ResultSet resultSet = hiveUtil.executeQuery(str);
        while (resultSet.next()){
            String jobName = resultSet.getString("job_name");
            String salary = resultSet.getString("salary");
            String address = resultSet.getString("address");
            String experience = resultSet.getString("experience");
            String education = resultSet.getString("education");
            String requirement= resultSet.getString("requirement");
            String[] requirementList = requirement.split("\\|");
            List<String> list = new ArrayList<>(Arrays.asList(requirementList));
            list.remove("<null>");
            String company = resultSet.getString("company");
            InfoVO build = new InfoVO().builder()
                    .jobName(jobName)
                    .salary(salary)
                    .address(address)
                    .experience(experience)
                    .education(education)
                    .requirement(list)
                    .company(company)
                    .build();
            infoVOS.add(build);
        }
        hiveUtil.close();
        return infoVOS;
    }*/
}
